package com.kun.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.kun.product.entity.ProductEntity;
import com.kun.product.service.ProductService;
import com.kun.common.utils.PageUtils;
import com.kun.common.utils.R;



/**
 * 
 *
 * @author kun_er
 * @email 2661038743@qq.com
 * @date 2022-05-02 16:11:15
 */
@RestController
@RequestMapping("product/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    /**
     * 根据分类id查询商品信息
     */
    @GetMapping("getProductByClassify/{id}")
    public R getProductByClassify(@PathVariable String id){
        List<ProductEntity> list = productService.list(new QueryWrapper<ProductEntity>().eq("cid", id));
        return R.ok().put("data",list);
    }
    /**
     * 查询所有商品
     */
    @GetMapping("getAllProduct")
    public R getAllProduct(){
        List<ProductEntity> list = productService.list(null);
        return R.ok().put("data",list);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = productService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id){
		ProductEntity product = productService.getById(id);

        return R.ok().put("product", product);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ProductEntity product){
		productService.save(product);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody ProductEntity product){
		productService.updateById(product);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] ids){
		productService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
